package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.LineTimePriceMapper;
import com.stylefeng.guns.modular.netcar.model.LineTimePrice;
import com.stylefeng.guns.modular.netcar.service.ILineTimePriceService;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 线路特定时间段加价 Service 实现 
 * @author 张亚林
 * @Date 2018-10-27 09:55
 */
@Service
public class LineTimePriceServiceImpl extends ServiceImpl<LineTimePriceMapper,LineTimePrice> implements ILineTimePriceService {

    @Resource
    private LineTimePriceMapper lineTimePriceMapper;

    /**
     * 删除线路特定时间段加价
     * @param lineTimePrice
     * @return     
     */
    @Transactional
    public int deleteLineTimePrice(LineTimePrice lineTimePrice,String token)throws Exception{
        if(lineTimePrice==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.lineTimePriceMapper.deleteLineTimePrice(lineTimePrice);
    }
    /**
     * 批量删除线路特定时间段加价
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchLineTimePrice(String[] ids,String token)throws Exception{
        return this.lineTimePriceMapper.deleteBatchLineTimePrice(ids);
    }
    /**
     * 查询线路特定时间段加价列表
     * @param lineTimePrice 线路特定时间段加价对象
     * @return
     */
    public List<LineTimePrice> selectListLineTimePrice(LineTimePrice lineTimePrice,String token)throws Exception{
        return  this.lineTimePriceMapper.selectListLineTimePrice(lineTimePrice);
    }
    /**
	 * 分页查询线路特定时间段加价列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListLineTimePrice(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(lineTimePriceMapper.selectListLineTimePrice(page,(LineTimePrice)pager.getObj()));
        List<LineTimePrice> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param lineTimePrice 线路特定时间段加价对象
     * @return 
     */
    public  List<LineTimePrice> selectListLineTimePriceForUpdate(LineTimePrice lineTimePrice)throws Exception{
        return  this.lineTimePriceMapper.selectListLineTimePriceForUpdate(lineTimePrice);
    }
    /**
     * 通过主键id查询的线路特定时间段加价列表
     * @param id 主键id
     * @return     
     */
    public  LineTimePrice findLineTimePriceById(String id,String token)throws Exception{
        return  this.lineTimePriceMapper.findLineTimePriceById(id);
    }

    /**
     * 新增线路特定时间段加价数据
     * @param lineTimePrice  线路特定时间段加价对象
     * @return
     */
    @Transactional
    public int insertLineTimePrice(LineTimePrice lineTimePrice,String token) throws Exception { 
        if(lineTimePrice!=null && (lineTimePrice.getLtpId()==null || lineTimePrice.getLtpId().trim().length()<10)){
            lineTimePrice.setLtpId(IdWorker.get32UUID());
        }  
        return this.lineTimePriceMapper.insertLineTimePrice(lineTimePrice);
    }
    
    /**
     * 更新线路特定时间段加价数据
     * @param lineTimePrice 线路特定时间段加价对象
     * @return     
     */
    @Transactional
    public int updateLineTimePrice(LineTimePrice lineTimePrice,String token) throws Exception {     	
        return  this.lineTimePriceMapper.updateLineTimePrice(lineTimePrice);
    }    
    /**
     * 查询线路特定时间段加价返回key-Value属性
     * @param lineTimePrice 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsLineTimePrice(LineTimePrice lineTimePrice,String token) throws Exception {
        List<LineTimePrice> listLineTimePrice=this.lineTimePriceMapper.selectListLineTimePrice(lineTimePrice);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(LineTimePrice item:listLineTimePrice){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getLtpId());
            opts.setValue(item.getOlId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
